package com.example.demo.flypig;

import com.example.demo.flypig.entity.People;

import java.util.*;
import java.util.stream.Collectors;

public class DemoList {


    /**
     * 生成测试数据
     *
     * @return
     */
    public static List<People> renderPeopleList() {
        //造数据
        List<People> peopleList = new ArrayList<>();
        People people = null;
        Random random = new Random();
        for (int i = 0; i < 1000; i++) {
            people = new People();
            people.setAge(random.nextInt(100) + 10);
            people.setName(UUID.randomUUID().toString().replace("-", ""));
            people.setId(Long.valueOf(i + 1));
            if (i % 2 == 0) {
                people.setSex("男");
            } else {
                people.setSex("女");
            }
            peopleList.add(people);
        }
        return peopleList;
    }


    /**
     * 排序
     *
     * @param peopleList
     */
    public static void sortPeople(List<People> peopleList) {
        /**
         * 排序 1.7
         */
        /*Collections.sort(peopleList, new Comparator<People>() {
            @Override
            public int compare(People o1, People o2) {
                return o1.getAge()-o2.getAge();
            }
        });*/
        /**
         * 排序 1.8
         */
        peopleList.sort(Comparator.comparing(People::getAge));
    }

    /**
     * 大于指定年龄
     *
     * @param peopleList
     * @param age
     */
    public static List<People> gtPeopleList(List<People> peopleList, Integer age) {
        /**
         * 大于30
         */
        peopleList = peopleList.stream().filter(p -> p.getAge() > age).collect(Collectors.toList());
        return peopleList;
    }


    /**
     * 分组
     *
     * @param peopleList
     * @return
     */
    public static Map<String, List<People>> groupByListBySex(List<People> peopleList) {
        return peopleList.stream().collect(Collectors.groupingBy(People::getSex));
    }

    /**
     * List 测试
     * <p>
     * <p>
     * 题目2：有一个list，里面存有1000个Person对象，Person对象的属性为Long id，String name，Integer age，String sex，请用jdk7、8特性编程，注意代码规范
     * 问题1：按照age进行排序。
     * 问题2：统计age大于30的人
     * 问题3：按照sex分组进行统计人数。
     * 问题4：按照sex进行分组。
     * 问题5：对问题4的人的name进行System.out.println打印。
     *
     * @param args
     */
    public static void main(String[] args) {
        List<People> peopleList = renderPeopleList();

        /**
         * 原始输出
         */
        //print(peopleList);


        /**
         * 排序
         */
        sortPeople(peopleList);
        //print(peopleList);


        /**
         * 大于30
         */
        peopleList = gtPeopleList(peopleList, 30);
        //print(peopleList);

        /**
         * 分组
         */
        Map<String, List<People>> peopleMapForSex = groupByListBySex(peopleList);
        /**
         * 不同性别数量
         */
        Map<String, Integer> countMap = new HashMap<>();
        Set<String> set = peopleMapForSex.keySet();
        Iterator<String> iter = set.iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            System.out.println("-------:" + key);
            List<People> list = peopleMapForSex.get(key);
            //放入map
            countMap.put(key, list.size());
            //普通打印观察
            print(list);
            /**
             * 问题5 打印name
             */
            list.forEach(x -> {
                System.out.println(x.getName());
            });
        }

        /**
         * 问题四输出
         */
        System.out.println(countMap.toString());


    }


    /**
     * 输出
     *
     * @param peopleList
     */
    public static void print(List<People> peopleList) {
        for (People people : peopleList) {
            System.out.println(people.toString());
        }
    }
}
